﻿@using Microsoft.AspNetCore.Components.Web.Virtualization
@namespace BootstrapBlazor.Components
@typeparam TItem
@inherits BootstrapModuleComponentBase
@attribute [BootstrapModuleAutoLoader(JSObjectReference = true)]

@if (Items == null)
{
    if (_init)
    {

    }
    else if (ShowSkeleton)
    {
        <SkeletonTree AdditionalAttributes="AdditionalAttributes"></SkeletonTree>
    }
    else
    {
        <div class="@LoadingClassString">
            <Spinner Color="Color.Primary" />
        </div>
    }
}
else
{
    <div @attributes="AdditionalAttributes" id="@Id" tabindex="0" class="@ClassString" data-bb-keyboard="@EnableKeyboardString">
        @if (ShowSearch)
        {
            @if (SearchTemplate == null)
            {
                <BootstrapInputGroup class="tree-search">
                    <BootstrapInput @bind-Value="@_searchText" OnEnterAsync="@OnEnterAsync" OnEscAsync="@OnEscAsync" />
                    @if (ShowResetSearchButton)
                    {
                        <Button Icon="@ClearSearchIcon" OnClickWithoutRender="OnClickResetSearch" class="tree-search-reset"></Button>
                    }
                    <Button Icon="@SearchIcon" OnClickWithoutRender="OnClickSearch"></Button>
                </BootstrapInputGroup>
            }
            else
            {
                <div class="tree-search">
                    @SearchTemplate
                </div>
            }
        }
        @if (IsVirtualize)
        {
            <div class="tree-root scroll is-virtual" tabindex="0">
                <Virtualize ItemSize="RowHeight" OverscanCount="OverscanCount" Items="@Rows">
                    @RenderRow(context)
                </Virtualize>
            </div>
        }
        else
        {
            <div class="tree-root scroll" tabindex="0">
                @foreach (var item in Rows)
                {
                    @RenderRow(item)
                }
            </div>
        }
    </div>
}

@code {
    RenderFragment RenderRow(TreeViewItem<TItem> item) =>
    @<TreeViewRow @key="item" IsActive="GetActive(item)" Index="GetIndex(item)" Item="item"
                  NodeIcon="@NodeIcon" ExpandNodeIcon="@ExpandNodeIcon" LoadingIcon="@LoadingIcon"
                  MaxSelectedCount="MaxSelectedCount"
                  ToolbarEditTitle="@ToolbarEditTitle" ToolbarEditLabelText="@ToolbarEditLabelText"
                  IsDisabled="IsDisabled" CanExpandWhenDisabled="CanExpandWhenDisabled"
                  ShowCheckbox="ShowCheckbox" ShowIcon="ShowIcon"
                  ShowToolbar="ShowToolbar" ShowToolbarCallback="ShowToolbarCallback"
                  OnToggleNodeAsync="OnToggleNodeAsync" OnClick="OnClick"
                  OnBeforeStateChangedCallback="OnBeforeStateChangedCallback"
                  OnCheckStateChanged="OnCheckStateChanged"
                  OnUpdateCallbackAsync="OnUpdateCallbackAsync" ToolbarTemplate="ToolbarTemplate"></TreeViewRow>;
}
